using System;
using System.Collections.Generic;
using System.Linq;
namespace DIS_WEB_Client.Controllers
{
	#region using
	using System.Web.Mvc;
	using DIS_WEB_Client.ViewModels;
	using DIS_WEB_Client.Repositories;
	using DIS_WEB_Client.Helpers;
	using System.Web.Security;
    using DIS_WEB_Client.DIS_BUS_WS;
	#endregion
	public partial class AdminController: BaseController
	{
        public ActionResult SelectItemBelongCategory(int idcate)
        {
            var data = new DataTransferViewModel
            {
                IDCategory = idcate,
                EnumViewModelType = EnumViewModel.ADMIN_ITEM,
            };
            return View("SelectItem", CreateViewModel(data));
        }

        public ActionResult DetailOfItem(int id)
        {
            var data = new DataTransferViewModel
            {
                IntID = id,
                IDItem = id,
                IDChemistWareHouse = _repChemistWareHouse.SelectOnlineWareHouse().ID,
                EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_ITEM,
            };
            return View(CreateViewModel(data));
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateItem(FormCollection forms)
        {
            var IDitem = int.Parse(forms.GetValues("Item_ID")[0]);
            var promotion = double.Parse(forms.GetValues("Item_Promotion")[0]);

            var onlinewarehouse = _repChemistWareHouse.SelectOnlineWareHouse().ID;
            var localProm = _repLocalPromotion.SelectByIDItemAndIDChemistWareHouse(IDitem, onlinewarehouse);
            var result = false;

            if (localProm == null)
            {
                localProm = new LocalPromotionDTO
                {
                    IDItem = IDitem,
                    IDChemistWareHouse = onlinewarehouse,
                    Discount = promotion,
                };
                result = BusService.Instance.LocalPromotion_Insert(localProm);
            }
            else
            {
                localProm.Discount = promotion;
                result = BusService.Instance.LocalPromotion_Update(localProm);
            }

            var data = new DataTransferViewModel
            {
                IntID = IDitem,
                IDItem = IDitem,
                IDChemistWareHouse = onlinewarehouse,
                EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_ITEM,
            };
            return View("DetailOfItem", CreateViewModel(data));
        }
	}
}